package com.happy.zebra.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happy.zebra.admin.api.dto.AllocateDTO;
import com.happy.zebra.admin.api.entity.OnlineAllocateRecordEntity;
import com.happy.zebra.admin.api.feign.RemoteCodeService;
import com.happy.zebra.admin.api.vo.AllocateVO;
import com.happy.zebra.admin.mapper.AllocateMapper;
import com.happy.zebra.admin.service.AllocateService;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.MultiAssist;
import com.shycloud.mido.member.api.feign.RemoteMemberService;
import com.shycloud.mido.member.api.util.Constant;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/allocate")
public class AllocateController {

	private AllocateMapper allocateMapper;
	private AllocateService allocateService;
	private RemoteCodeService remoteCodeService;
	private RemoteMemberService remoteMemberService;

	@GetMapping("/page")
	public R page(Page<AllocateVO> page, Integer type, String name, String startDate, String endDate) {
		//根据type来区分三种类别的数据
		//1: 运营商; 2:机构; 3:工作室
		switch (type) {
			case 1:
				return new R<>(allocateMapper.getOptPage(page, name, startDate, endDate));
			case 2:
				return new R<>(allocateMapper.getOrgPage(page, name, startDate, endDate));
			case 3:
				return new R<>(allocateMapper.getStudioPage(page, name, startDate, endDate));
			default:
				return new R<>();
		}
	}

	@GetMapping("/getOnlinePage")
	public R getOnlinePage(Page<AllocateVO> page, Integer type, Integer optId, String name, String phone) {

		//根据type来区分三种类别的数据

		//1: 运营商; 2:机构; 3:工作室
		switch (type) {
			case 1:
				IPage<AllocateVO> onlineOptPage = allocateMapper.getOnlineOptPage(page, name, phone);
				for (AllocateVO record : onlineOptPage.getRecords()) {
					MultiAssist assistCount = remoteMemberService
							.getAssistCount(Constant.ASSIST_SUBJECT_OPT, String.valueOf(record.getId()));
					record.setAssistCount(assistCount == null ? 0 : assistCount.getMonthly());
				}
				return new R<>(onlineOptPage);
			case 2:
				IPage<AllocateVO> onlineOrgPage = allocateMapper.getOnlineOrgPage(page, name, optId, phone);
				for (AllocateVO record : onlineOrgPage.getRecords()) {
					MultiAssist assistCount = remoteMemberService
							.getAssistCount(Constant.ASSIST_SUBJECT_ORG, String.valueOf(record.getId()));
					record.setAssistCount(assistCount == null ? 0 : assistCount.getMonthly());
				}
				return new R<>(onlineOrgPage);
			case 3:
				IPage<AllocateVO> onlineStudioPage = allocateMapper.getOnlineStudioPage(page, name, optId, phone);
				for (AllocateVO record : onlineStudioPage.getRecords()) {
					MultiAssist assistCount = remoteMemberService
							.getAssistCount(Constant.ASSIST_SUBJECT_STUDIO, String.valueOf(record.getId()));
					record.setAssistCount(assistCount == null ? 0 : assistCount.getMonthly());
				}
				return new R<>(onlineStudioPage);
			default:
				return new R<>();
		}
	}

	@Transactional
	@GetMapping("/allocateOnline")
	public R allocateOnline(Integer type, Integer myOptId, Integer targetId, Integer duration) {
		//type: 1-总部给运营商分配, 2-总部/运营商给机构/工作室分配
		if (type == 1) {
			allocateMapper.allocateOnlineOpt(targetId, duration);
			//插入分配记录
			OnlineAllocateRecordEntity record = new OnlineAllocateRecordEntity();
			record.setOnlineDuration(duration);
			record.setOptId(myOptId);
			record.setTargetOptId(targetId);
			allocateMapper.insertOnlineRecord(record);
		}
		if (type == 2) {
			//如果是运营商则扣除运营商课时数
			allocateMapper.allocateOnlineOpt(myOptId, duration * (-1));
			//开始分配
			allocateMapper.allocateOnlineOrg(myOptId, targetId, duration);
			//插入分配记录
			OnlineAllocateRecordEntity record = new OnlineAllocateRecordEntity();
			record.setOnlineDuration(duration);
			record.setOptId(myOptId);
			record.setTargetOrgId(targetId);
			allocateMapper.insertOnlineRecord(record);
		}
		return new R<>(true);
	}

	@GetMapping("/getOnlineRecord")
	public R getOnlineRecord(Page<OnlineAllocateRecordEntity> page, Integer optId, Integer targetOptId, Integer targetOrgId,
			String startDate, String endDate, String name, String stockOptName) {
		return new R<>(allocateMapper.getOnlineRecord(page, optId, targetOptId, targetOrgId, startDate, endDate, name, stockOptName));
	}

	@GetMapping("/getOnlineCourseAvail")
	public R getOnlineCourseAvail(Integer optId, Integer orgId) {
		Integer onlineCourseAvail = allocateMapper.getOnlineCourseAvail(optId, orgId);
		return new R<>(onlineCourseAvail == null ? 0 : onlineCourseAvail);
	}

	@ApiOperation("总部分配智能设备列表接口")
	@GetMapping("/getSurplus")
	public R getSurplus(Integer type, Integer orgId, Integer optId) {
		//根据type来区分三种类别的数据
		//1: 总部->运营商,机构,工作室; 2:运营商->机构;
		List result = new ArrayList<>();
		Map item0 = new HashMap<>();
		Map item1 = new HashMap<>();
		Map item2 = new HashMap<>();
		switch (type) {
			case 1:
				item0.put("goodId", "3");
				item0.put("duration", 90);
				item0.put("name", "智能设备90天使用权限");
				item0.put("amount", "-");
				item0.put("targetNum", "0");

				item1.put("goodId", "4");
				item1.put("duration", 180);
				item1.put("name", "智能设备180天使用权限");
				item1.put("amount", "-");
				item1.put("targetNum", "0");

				item2.put("goodId", "5");
				item2.put("duration", 360);
				item2.put("name", "智能设备360天使用权限");
				item2.put("amount", "-");
				item2.put("targetNum", "0");
				result.add(item0);
				result.add(item1);
				result.add(item2);
				break;
			case 2:
				item0.put("goodId", "3");
				item0.put("duration", 90);
				item0.put("name", "智能设备90天使用权限");
				item0.put("targetNum", "0");
				try {
					item0.put("amount", allocateMapper.getStockByGoodsId(3, optId, orgId));
				} catch (Exception e) {
					item0.put("amount", "0");
				}

				item1.put("goodId", "4");
				item1.put("duration", 180);
				item1.put("name", "智能设备180天使用权限");
				item1.put("targetNum", "0");
				try {
					item1.put("amount", allocateMapper.getStockByGoodsId(4, optId, orgId));
				} catch (Exception e) {
					item1.put("amount", "0");
				}

				item2.put("goodId", "5");
				item2.put("duration", 360);
				item2.put("name", "智能设备360天使用权限");
				item2.put("targetNum", "0");
				try {
					item2.put("amount", allocateMapper.getStockByGoodsId(5, optId, orgId));
				} catch (Exception e) {
					item2.put("amount", "0");
				}
				result.add(item0);
				result.add(item1);
				result.add(item2);
			default:
				break;
		}
		return new R<>(result);
	}

	@Transactional
	@PostMapping("/allocate")
	public R allocate(@RequestBody AllocateDTO dto) {
		//以<时长 : 数量>列表作为参数来进行激活码的分配
		for (Map<String, Integer> map : dto.getCodeList()) {
			Integer duration = map.get("duration");
			Integer number = map.get("targetNum");
			Integer goodsId = map.get("goodId");
			//批量生成激活码(时长, 数量)
			//总部给运营商分配
			if (null != dto.getOptId() && null == dto.getOrgId()) {
				remoteCodeService.allocate(duration, number, dto.getOptId(), null);
				allocateService.updateStock(goodsId, dto.getOptId(), dto.getOrgId(), number, duration);
			}
			//总部给机构分配
			if (null != dto.getOrgId() && null == dto.getOptId()) {
				remoteCodeService.allocate(duration, number, null, dto.getOrgId());
				allocateService.updateStock(goodsId, dto.getOptId(), dto.getOrgId(), number, duration);
			}
			//运营商分配给机构
			if (null != dto.getOptId() && null != dto.getOrgId()) {
				allocateMapper.allocateFromOptToOrg(duration, number, dto.getOptId(), dto.getOrgId());
				//增加机构库存
				allocateService.updateStock(goodsId, null, dto.getOrgId(), number, duration);
				//减少运营商库存
				allocateService.updateStock(goodsId, dto.getOptId(), null, (number * -1), duration);
			}
		}
		//分配UDID
		if (null != dto.getOptId() && null != dto.getOrgId() && dto.getUdidList().size() != 0) {
			allocateMapper.allocateUDID(dto.getUdidList(), dto.getOptId(), dto.getOrgId());
		}
		return new R<>();
	}

	@GetMapping("/checkUDID")
	public R checkUDID(String udid, Integer optId) {
		if (allocateMapper.getUDID(udid, optId) > 0) {
			return new R<>(true);
		} else {
			return new R<>(false);
		}
	}
}